//
//  KotnetLogin.m
//  KotnetAutologin
//
//	login to kotnet
//
//  Created by Kristof Marien on 10/01/10.
//  Copyright 2010 KotnetAutologin. All rights reserved.
//

#import "KotnetLogin.h"


@implementation KotnetLogin

@synthesize dataHTML;
@synthesize delegate;
@synthesize cannotLoginCallback;
@synthesize loggedInCallback;

/**
 *  starts loggin in
 */
- (void) login:(id)loginDelegate cannotLoginSelector:(SEL)cannotLoginSelector loggedInSelector:(SEL)loggedInSelector
{
	self.delegate = loginDelegate;
	self.cannotLoginCallback = cannotLoginSelector;
	self.loggedInCallback = loggedInSelector;
	
	NSString *user = [[[NSUserDefaultsController sharedUserDefaultsController] values] valueForKey:@"userName"];
	KeyChain *keychain = [KeyChain alloc];
	NSString *pwd = [keychain getPasswordKeyChain:user];
	[keychain release];
	
	if (pwd == @"not found PWD") {
		if (self.delegate && self.cannotLoginCallback) {
			if ([self.delegate respondsToSelector:self.cannotLoginCallback]) {
				[self.delegate performSelector:self.cannotLoginCallback withObject:@"Geen gebruiker ingesteld"];
			}
		}
		return;
	}
	
	source = [GetHTMLSource alloc];
	source.url = @"https://netlogin.kuleuven.be/cgi-bin/wayf.pl?inst=kuleuven&lang=nl&submit=Ga+verder+%2F+Continue";
	[source getSource:self receivedSelector:@selector(sourcePWDFieldReceived:) errorSelector:@selector(sourcePWDFieldNotReceived)];
}
/**
 *  source received, extract pwdfield name
 */
- (void)sourcePWDFieldReceived:(NSString *)data
{
	[source release];
	NSString *searchString = @"<input type=\"password\" size=\"20\" maxlength=\"20\" name=\"";
	NSRange range = [data rangeOfString:searchString];
	if (range.location == NSNotFound) {
		[self sourcePWDFieldNotReceived];
		return;
	}
	NSString *user = [[[NSUserDefaultsController sharedUserDefaultsController] values] valueForKey:@"userName"];
	KeyChain *keychain = [KeyChain alloc];
	NSString *pwd;
	do {
		pwd = [keychain getPasswordKeyChain:user];
	} while ([pwd length] < 2);
	[keychain release];
	NSString *pwdField = [data substringFromIndex:range.location+[searchString length]];
	range = [pwdField rangeOfString:@"\""];
	pwdField = [pwdField substringToIndex:range.location];
	
	source = [GetHTMLSource alloc];
	source.url = @"https://netlogin.kuleuven.be/cgi-bin/netlogin.pl";
	source.data = [NSString stringWithFormat:@"inst=kuleuven&lang=nl&uid=%@&%@=%@&submit=Login", user, pwdField, pwd];
	[source getSource:self receivedSelector:@selector(loginSended:) errorSelector:@selector(sourcePWDFieldNotReceived)];
}
/**
 *  error getting password field name
 */
- (void)sourcePWDFieldNotReceived
{
	[source release];
	if (self.delegate && self.cannotLoginCallback) {
		if ([self.delegate respondsToSelector:self.cannotLoginCallback]) {
			[self.delegate performSelector:self.cannotLoginCallback withObject:@"Onbekende fout"];
		}
	}
}
/**
 *  check source loggin in
 */
- (void)loginSended:(NSString *)data
{
	[source release];
	self.dataHTML = data;
	NSRange range = [data rangeOfString:@"Login geslaagd."];
	if (range.location == NSNotFound) {
		if (self.delegate && self.cannotLoginCallback) {
			if ([self.delegate respondsToSelector:self.cannotLoginCallback]) {
				[self.delegate performSelector:self.cannotLoginCallback withObject:@"Fout tijdens inloggen"];
			}
		}
	} else {
		if (self.delegate && self.loggedInCallback) {
			if ([self.delegate respondsToSelector:self.loggedInCallback]) {
				[self.delegate performSelector:self.loggedInCallback withObject:data];
			}
		}
	}
}

@end
